home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 47.7z / BS1 part 47 / ImageMaster RT v1.50b (1994)(Black Belt Systems)(Disk 6 of 7)[HD].7z / ImageMaster RT v1.50b (1994)(Black Belt Systems)(Disk 6 of 7)[HD].adf / piarc.lzh.parta / jpeg.rexx < prev    next >
OS/2 REXX Batch file  |  1994-03-17  |  10KB  |  404 lines

  1. /*
  2.  * JPEG.rexx
  3.  *
  4.  *  Written by: Ben Williams
  5.  * Last Update: September 20th, 1993
  6.  *         For: Black Belt Systems image processing series IM, IM F/c, and IP.
  7.  * ---------------------------------------------------------------------------
  8.  *    Revision: 1.13
  9.  */
  10. parse arg action '"' fullname '"' buffer
  11.  
  12. /*
  13.  * open rexxsupport.library -- needed for some functions
  14.  */
  15. if ~show('L',"rexxsupport.library") then do
  16.   if addlib('rexxsupport.library',0,-30,0) then do
  17.       /* everything's ok */
  18.     end;
  19.   else do
  20.     say 'We Have A Library Problem, Unable To Load "rexxsupport.library"';
  21.     say 'Cannot operate JPEG.rexx without this library - sorry!';
  22.     'finish';
  23.     exit 10;
  24.     end;
  25.   end;
  26.  
  27. /*
  28.  * This will automatically direct the script to the proper
  29.  * software, if it is running.
  30.  */
  31. prtnme = 'IP_Port'; /* assume Image Professional */
  32. if show('P','IP_Port') = 0 then do
  33.   if show('P','IM_Port') = 0 then do
  34.     say "Can't find image processor's ARexx port!!!"; /* not running? */
  35.     say "This script requires IP, IM or IM F/c to run!";
  36.     exit(20);
  37.     end;
  38.   else do
  39.     prtnme = 'IM_Port'; /* That's the thing about assumptions... */
  40.     end;                 /* We make em, user's break em.          */
  41.   end;
  42.  
  43.   /*
  44.    * This code attempts to read a file called "picmdpath" from REXX:
  45.    * If it can't find it, the script will assume that the commands
  46.    * associated with this PI Module are in "c:". If the file exists,
  47.    * the script will look in the path that is specified in the file.
  48.    * If you create this file, you MUST put a complete, correct path
  49.    * in it; if the commands are in a sub-directory, you have to put
  50.    * the trailing slash on the path (like, device:dir/).
  51.    * 
  52.    */
  53.   cmdpath = 'c:';
  54.   if open(fhandle,'rexx:picmdpath','read') then  /* open the file */
  55.     do
  56.       cmdpath = readln(fhandle);
  57.       call close(fhandle);  /* close the file    */
  58.     end
  59.  
  60. /*
  61.  * Prompt user - load, or save?
  62.  */
  63. if action = 'load' then do
  64.   pick = 1
  65.   end
  66. else if action = 'save' then do
  67.   pick = 2
  68.   end
  69. else do
  70.     address(prtnme);
  71.     options results;
  72.     'gadgets "Load","JFIF/JPEG","Save","JFIF/JPEG"';
  73.     pick = result;
  74.     options;
  75.     address;
  76.     end;
  77.  
  78. if pick=0 then do
  79.   exit 0;
  80.   end;
  81.  
  82. /*
  83.  * PI driver for compressor
  84.  */
  85. if pick=2 then do /* compression */
  86.  
  87.   bufname = 'image';
  88.   strn = ' -q ';
  89.   address(prtnme);
  90.  
  91.   options results;
  92.   'askprop "Compression/Damage Factor","0","0","100"';
  93.   qfact = result;
  94.   'askyn "Good compression","Best Compression"';
  95.   compr = result;
  96.   options;
  97.  
  98.   if compr = 1 then do
  99.     strn = ' -E'||strn;
  100.     end;
  101.  
  102.   prevpath = 'ram:'; /* put user in ram to start with... */
  103.   if show('C',jpegpath) = 1 then do
  104.     prevpath = getclip(jpegpath);
  105.     end;
  106.  
  107.   options results;
  108.   'current';
  109.   bufdata = result; /* get name of buffer, if there is one */
  110.   parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
  111.   if bname ~= '<none>' then do
  112.     bufname = bname;
  113.     end;
  114.   if (length(bufname) > 4) then do
  115.     epos = pos('.jpg',bufname,length(bufname)-4);
  116.     if epos ~= 0 then do
  117.       bufname = left(bufname,epos-1)
  118.       end
  119.     end;
  120.   if fullname = '' then do
  121.       'filerequest "'||prevpath||'","'||bufname||'",".jpg","Save JPEG"';
  122.       jpegfile = result;
  123.       end;
  124.   else do
  125.       jpegfile = fullname;
  126.       end;
  127.   
  128.   'getdpi '||bnum;
  129.   parse var result fxdpi ',' fydpi; /* get image DPI to write to file */
  130.   options;
  131.   
  132.   /*
  133.    * This strips the floating point info to integer portions only:
  134.    */
  135.   parse var fxdpi xdpi '.' fractional
  136.   parse var fydpi ydpi '.' fractional
  137.   xdpi = strip(xdpi,'B',' ');
  138.   ydpi = strip(ydpi,'B',' ');
  139.   tline = xdpi||','||ydpi;
  140.   /*
  141.    * create temp file:
  142.    */
  143.   call open(thandle,'ram:jp_dpi_temp','write');
  144.     writeln(thandle,tline);
  145.     call close(thandle);
  146.  
  147.   if jpegfile = 'FR_CANCELLED' then do
  148.     address(prtnme);
  149.     exit 0;
  150.     end;
  151.  
  152. jpegfile = expandfilename(jpegfile); /* make filename complete path */
  153. thispath = gimmepath(jpegfile);
  154. call setclip(jpegpath,thispath);
  155.  
  156.   address(prtnme);
  157.   if buffer = '' then do
  158.       options results;
  159.       'jackin';
  160.       jackadr = result;
  161.       options;
  162.       end;
  163.   else do
  164.       options results;
  165.       'backin '||buffer;
  166.       jackadr = result;
  167.       options;
  168.       end;
  169.  
  170.   address command cmdpath||'wrjpg >"'||jpegfile||'" -j'||jackadr||strn||qfact;
  171.   address command "delete ram:jp_dpi_temp QUIET";
  172.  
  173.   address(prtnme);
  174.   'finish';
  175.   address;
  176.  
  177.   exit 0;
  178.  
  179.   end;
  180.  
  181. else do /* pick=1 decompression */
  182.  
  183. if fullname = "" then do
  184.     /*
  185.      * Setup default path
  186.      */
  187.       prevpath = 'ram:'; /* put user in ram to start with... */
  188.     /*
  189.      * Now, get old path if it exists
  190.      */
  191.       if show('C',jpegpath) = 1 then do
  192.         prevpath = getclip(jpegpath);
  193.         end;
  194.     
  195.       address(prtnme);
  196.       options results;
  197.       'current';
  198.       bufdata = result; /* get name of buffer, if there is one */
  199.       parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
  200.       if bname ~= '<none>' then do
  201.         bufname = bname;
  202.         end;
  203.       'filerequest "'||prevpath||'","'||bufname||'","","Load JPEG"';
  204.       jpegfile = result;
  205.       options;
  206.     
  207.       if jpegfile = 'FR_CANCELLED' then do
  208.         address(prtnme);
  209.         'finish';
  210.         exit 0;
  211.         end;
  212.     
  213.       jpegfile = expandfilename(jpegfile);
  214.     end;
  215. else do /* fullname defined on entry to the macro */
  216.   jpegfile = expandfilename(fullname);
  217. end;
  218.   thispath = gimmepath(jpegfile);
  219.   call setclip(jpegpath,thispath);
  220.  
  221.   fileinfo = statef(jpegfile);
  222.   parse var fileinfo fitype fibytes fiblocks fiflags fidays fimins fiticks ficomment;
  223.  
  224.   if fitype = '' then do
  225.     'message Cannot locate "'jpegfile'" for processing';
  226.     'finish';
  227.     exit 10;
  228.     end;
  229.  
  230.   if fibytes < 170 then do
  231.     "message This file is too small to be a JFIF formatted JPEG file";
  232.     'finish';
  233.     exit 10;
  234.     end;
  235.  
  236.   if fitype = 'DIR' then do
  237.     'message Must specify a file, not a directory';
  238.     'finish';
  239.     exit 10;
  240.     end;
  241.  
  242. /*
  243.  * at this point, we have at least some assurance that we
  244.  * have a real JPEG file to work with. Now, we need to look into
  245.  * the file and see how big the image is, so we can open a new
  246.  * buffer of the appropriate size.
  247.  */
  248.  
  249.   call open(fhandle,jpegfile,'read');      /* open the file     */
  250.   if rvalue() ~= 65496 then do /* FFD8 */
  251.     'message Not a JFIF file! - Initial read fails';
  252.     call close(fhandle);
  253.     'finish';
  254.     exit(0);
  255.     end;
  256.  
  257.   work=1;
  258.   do while work = 1
  259.     thisid = rvalue();
  260.     thisln = rvalue();
  261.     thisda = readch(fhandle,thisln-2);
  262.     select
  263.       when thisid = 65472 | thisid < 65488 then do /* FFC0-FFCF (SOF0-15) */
  264.         height = c2d(substr(thisda,2,2));
  265.         width  = c2d(substr(thisda,4,2));
  266.         if thisid ~= 65476 then do   /* 65476 should be excluded */
  267.           work=0;
  268.           end;
  269.         end;
  270.       when thisid = 65504 then do  /* FFE0 */
  271.         fid = left(thisda,4);
  272.         if fid ~= 'JFIF' then do
  273.           'message Not a JFIF file!';
  274.           call close(fhandle);
  275.           'finish';
  276.           exit 0;
  277.           end;
  278.         end;
  279.       otherwise do
  280.         nop;
  281.         end;
  282.       end;
  283.     end;
  284.  
  285.   call close(fhandle);                     /* close the file    */
  286.  
  287.   if height < 0 then do
  288.     "message Bad height: "||height;
  289.     'finish';
  290.     exit 0;
  291.     end;
  292.  
  293.   if height > 32767 then do
  294.     "message Bad height: "||height;
  295.     'finish';
  296.     exit 0;
  297.     end;
  298.  
  299.   if width < 0 then do
  300.     "message Bad width: "||width;
  301.     'finish';
  302.     exit 0;
  303.     end;
  304.  
  305.   if width > 32767 then do
  306.     "message Bad width: "||width;
  307.    'finish';
  308.     exit 0;
  309.     end;
  310.  
  311.   address(prtnme);
  312.   
  313.   /* New buffer is created at current resolution */
  314.   address(prtnme);
  315.   'autoredraw 0';
  316.   
  317.   options results;
  318.   
  319.   'newtargetted '||width||' '||height||' "'||gxname||'"'
  320.   if rc ~= 0 then do
  321.     options;
  322.     "message Can't allocate buffer, error #"||rc;
  323.     'autoredraw 1';
  324.     'finish';
  325.     exit 0;
  326.     end
  327.   bnum = result;
  328.   
  329.   'backin '||bnum;
  330.   jackadr = result;
  331.   options;
  332.  
  333.   'lockimage '||bnum;
  334.   address command cmdpath||'rdjpg -j'||jackadr||' "'||jpegfile||'"';
  335.   call open(thandle,'ram:jp_dpi_temp','read');      /* open the file     */
  336.  
  337.     dpidpi = readln(thandle);
  338.     call close(thandle);
  339.     
  340.   parse var dpidpi xdpi ',' ydpi;
  341.   'setdpi '||bnum||' '||xdpi||' '||ydpi;
  342.   address command "delete ram:jp_dpi_temp QUIET";
  343.   'unlockimage '||bnum;
  344.  
  345.   address(prtnme);
  346.   'autoredraw 1';
  347.   'finish';
  348.   address;
  349.  
  350.   exit 0;
  351.  
  352. end;
  353.  
  354. /*
  355.  * gimmepath
  356.  *
  357.  * This takes the provided argument and sucks the path out of it, then
  358.  * returns that path to the caller, sans file name.
  359.  */
  360. gimmepath:
  361.   arg fullnamegx;
  362.     tempgx = reverse(fullnamegx);
  363.     lengx = length(fullnamegx);   /* get length of string */
  364.     slashdex = index(tempgx,'/'); /* first occurance of '/' from right */
  365.     colondex = index(tempgx,':');  /* first occurance of ':' from right */
  366.     seploc = 0; /* assumes current dir, no path supplied */
  367.     if slashdex ~= 0 then do /* we assume we are in a DIR */
  368.       seploc = (lengx - slashdex)+1;
  369.       end;
  370.     else do
  371.       if colondex ~= 0 then do /* we assume we are on a device */
  372.         seploc = (lengx - colondex)+1;
  373.         end;
  374.       end;
  375.   gxname = substr(fullnamegx,seploc+1); /* if you ever need it */
  376.   gxpath = left(fullnamegx,seploc);
  377.   return(gxpath);
  378.  
  379. /*
  380.  * Since this script can't be expected to know where the CD of the user
  381.  * is when this cmd is invoked, we have to check the path the user
  382.  * provides - if it's not specified right from a root, then we have
  383.  * to make it a complete specification from the root.
  384.  */
  385. expandfilename:
  386.   parse arg jfile;
  387.   if index(jfile,':') = 0 then do
  388.     curdir = pragma(D);
  389.     if right(curdir,1) ~= ':' then do
  390.       if right(curdir,1) ~= '/' then do
  391.         if curdir ~= '' then do
  392.           curdir = curdir || '/';
  393.           end;
  394.         end;
  395.       end;
  396.     jfile = curdir||jfile;
  397.     end;
  398.   return(jfile);
  399.  
  400. rvalue:
  401.   wordnum = c2d(readch(fhandle,1)) * 256;
  402.   wordnum = wordnum + c2d(readch(fhandle,1));
  403.   return wordnum;
  404.